/*
*    Author : heiden deng(dengjianquan@beyondcent.com)
*    2017/05/15
*    version 0.0.9
*    配置文件操作实现方法
*/

#include "htsc_grpc_properties_tools.h"
#include "htsc_grpc_utils.h"
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>

#ifndef WIN64
	#include<unistd.h>
#else
	#include<direct.h>
#endif


#define HTSC_GRPC_PROPERTIES_LINUX_DEFAULT_PATH  "/etc/"
#define HTSC_GRPC_PROPERTIES_DEFAULT_DIR "conf"


typedef struct _properties_param_t {
	char key[HTSC_GRPC_PROPERTY_KEY_MAX_LEN];
	char value[HTSC_GRPC_PROPERTY_VALUE_MAX_LEN];
}properties_param_t;

static bool properties_inited = false;
static properties_param_t properties[HTSC_GRPC_MAX_PERPERTIES_NUM];
static int properties_num = 0;


// 从字符串中提取key、value到缓存区中
int get_property_value(char *keyAndValue, char * key, char * value) {

	char *p = keyAndValue;
	char *pValue = NULL;
	char *key_start = keyAndValue;
	char *value_start = NULL;
	int line_len = strlen(keyAndValue);
	if (!keyAndValue || !key || !value || line_len < 3 || keyAndValue[0] == '#')
	{
		return -2;
	}
	p = strstr(keyAndValue, "=");
	if (p == NULL) {
		//printf("没有=\n");
		return -1;
	}
	value_start = p + 1;
	*p = '\0';
	trim(key_start, key);

	trim(value_start, value);

	return 0;
}

int htsc_grpc_properties_init_inner(const char *filename) {
	FILE *pf = NULL;
	char line[HTSC_GRPC_BUF_LEN] = { 0 };
	int index = 0;
	if (properties_inited)
	{
		return 1;
	}
	pf = fopen(filename, "r");
	if (!pf)
	{
		printf("open %s failed\n", filename);
		return -1;
	}
	while (!feof(pf)) {
		memset(line, 0, HTSC_GRPC_BUF_LEN);
		fgets(line, HTSC_GRPC_BUF_LEN, pf);
		if (0 == get_property_value(line, properties[index].key, properties[index].value)) {
			index++;
		}
	}
	fclose(pf);
	properties_num = index;
	properties_inited = true;
	return 0;
}

int htsc_grpc_properties_init() {
	char *p = NULL,*project_path = NULL;
	char conf_path[HTSC_GRPC_PATH_MAX_LEN] = {0};
	int ret = 0;
	int path_len = 0;
	int filename_len = strlen(HTSC_GRPC_PROPERTIES_FILENAME);
	int default_dir_len = strlen(HTSC_GRPC_PROPERTIES_DEFAULT_DIR);
#ifndef WIN64
	int separator_len = 1;
#else
	int separator_len = 2;
#endif
	if (properties_inited)
	{
		printf("properties has initialized\n");
		return 1;
	}
	p = _getcwd(conf_path, HTSC_GRPC_PATH_MAX_LEN);
	path_len = strlen(conf_path);
	if (path_len + strlen(HTSC_GRPC_PROPERTIES_FILENAME) >= HTSC_GRPC_PATH_MAX_LEN)
	{
		return -1;
	}
	project_path = strrchr(conf_path, PATH_SEPRATOR);
	p = conf_path + path_len;

	//处理./xxx.conf文件 ,
	snprintf(p, separator_len + filename_len + 1, "%c%s", PATH_SEPRATOR, HTSC_GRPC_PROPERTIES_FILENAME);
	if (0 == htsc_grpc_properties_init_inner(conf_path))
	{
		return 0;
	}

	//处理./conf/xxx.conf文件
	memset(p, 0, HTSC_GRPC_PATH_MAX_LEN - path_len);
	snprintf(p,2 * separator_len + default_dir_len + filename_len + 1, "%c%s%c%s", PATH_SEPRATOR, HTSC_GRPC_PROPERTIES_DEFAULT_DIR,
												PATH_SEPRATOR, HTSC_GRPC_PROPERTIES_FILENAME);
	if (0 == htsc_grpc_properties_init_inner(conf_path))
	{
		return 0;
	}

	//处理../conf/xxx.conf文件,当在exe目录下执行时
	p = project_path;
	memset(p, 0, HTSC_GRPC_PATH_MAX_LEN - (p - conf_path));
	snprintf(p, separator_len + default_dir_len + filename_len + 1, "%s%c%s", HTSC_GRPC_PROPERTIES_DEFAULT_DIR, PATH_SEPRATOR,
												HTSC_GRPC_PROPERTIES_FILENAME);
	if (0 == htsc_grpc_properties_init_inner(conf_path))
	{
		return 0;
	}
#ifndef WIN64
	memset(conf_path, 0, HTSC_GRPC_PATH_MAX_LEN);
	snprintf(conf_path, strlen(HTSC_GRPC_PROPERTIES_LINUX_DEFAULT_PATH) + filename_len + 1, "%s%s",
										HTSC_GRPC_PROPERTIES_LINUX_DEFAULT_PATH,
										HTSC_GRPC_PROPERTIES_FILENAME);
	if (0 == htsc_grpc_properties_init_inner(conf_path))
	{
		return 0;
	}

#endif
	return -2;
}

int htsc_grpc_properties_get_value(const char *key, char *prefix,char *value) {
	char buf[HTSC_GRPC_BUF_LEN];
	int index = 0;
	char *pOut = value;
	memset(buf, 0, HTSC_GRPC_BUF_LEN);
	for (index = 0; index < properties_num; index++) {
		if (0 == strcmp(key, properties[index].key))
		{		
			snprintf(pOut, strlen(properties[index].value) + 1, "%s", properties[index].value);
			return 0;
		}
		//处理有前缀的key情形
		if (prefix)
		{
			memset(buf, 0, HTSC_GRPC_BUF_LEN);
			snprintf(buf, strlen(prefix) + strlen(key) + 1, "%s%s", prefix, key);
			if (0 == strcmp(buf, properties[index].key))
			{
				snprintf(pOut, strlen(properties[index].value) + 1, "%s", properties[index].value);
				return 0;
			}
			memset(buf, 0, HTSC_GRPC_BUF_LEN);
			snprintf(buf, strlen(prefix) + strlen(properties[index].key) + 1, "%s%s", prefix, properties[index].key);
			if (0 == strcmp(buf, key))
			{
				snprintf(pOut, strlen(properties[index].value) + 1, "%s", properties[index].value);
				return 0;
			}
		}
		
	}
	return 1;
}